** Written by: Bryan Parthum; bparthum@gmail.com ; March 2017
* Edited by Mateus Souza; May 2021

********************************************************************************
*******************  PRInceton Scorekeeping Method - PRISM  ********************
********************************************************************************

use "$maindir/Data/ihwap_degreedays.dta", clear
drop if mmbtu==.

** Generate necessary variables and interactions
gen lnmmbtu = log(mmbtu+0.001)
tostring ProgramYear, replace
encode ProgramYear, gen(PY)
gen treat_PY = treat*PY
gen home_treat_PY = home_treat*PY

** Generate variables to store R-squared
* HDD only 
forv i = 40/75 {
gen rsqHDD`i' =.
}


** Generate period of construction 
gen treat_period = "post" if treated==1
replace treat_period = "pre" if treated==0
replace treat_period = "const" if treated==.


** prism_gas.dta
keep if fueltype=="gas"


********************************************************
******************************************  Prism Step 1 
******************************************      HDD ONLY
********************************************************

** Find Rsquared for every HDD
su home_treat, meanonly
forv i = `r(min)'/`r(max)' {
	qui : sum mmbtu [aw=meterdays] if home_treat==`i'
	sca nobs = r(N)
	if nobs >=2 {
		forv j = 40/75 {
			qui : reg mmbtu HDD`j' [aw=meterdays] if home_treat==`i'
			replace rsqHDD`j'=e(r2) if home_treat==`i'
		}
	}
}


** Find max Rsquared
gen which_max = "" 
gen max = 0 
unab xvars : rsqHDD* 
foreach x of local xvars { 
    replace which_max = "`x'" if `x' > max 
	replace max = `x' if `x' > max 
}

** Keep the highest Rsquared
gen HDDbest = substr(which_max,7,2)
destring HDDbest, replace

save "$maindir/Results/Model_Outputs/prism_gas.dta", replace
